package com.youdianstar.app.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.youdianstar.app.R
import com.youdianstar.app.entity.CouponRes
import com.youdianstar.app.util.DataAppManager
import com.youdianstar.app.common.util.DateUtil
import com.youdianstar.app.common.util.StrUtil
import com.youdianstar.app.common.widget.SheetCheckHelper
import com.youdianstar.app.util.DataHelperEx


/**
 * File: CouponSelectAdapter
 * Author: 82149 Create: 2022/3/4 10:08
 * Changes (from 2022/3/4)
 * --------------------------------------------------
 * description: 优惠券选择 (旧版)
 * ---------------------------------------------------
 *
 */
class CouponSelectAdapter(val context: Context, val dataList: List<CouponRes>, val selectHelper: SheetCheckHelper) : RecyclerView.Adapter<CouponSelectAdapter.SheetHolder>(){

    class SheetHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
        val tvTitle = itemView.findViewById<TextView>(R.id.item_title)
        val tvDate = itemView.findViewById<TextView>(R.id.item_tv_date)
        val tvAddress = itemView.findViewById<TextView>(R.id.item_tv_address)

        val tvUse = itemView.findViewById<TextView>(R.id.state_use)
        val ivSelect = itemView.findViewById<ImageView>(R.id.state_select)
        val rlSelected = itemView.findViewById<RelativeLayout>(R.id.ll_state)
    }

    private var symbolTx = ""
    init {
        symbolTx = DataAppManager.instance.symbolTx()
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SheetHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.layout_item_coupon_select, parent, false)
        return SheetHolder(view)
    }

    override fun onBindViewHolder(holder: SheetHolder, position: Int) {
        dataList[position].let {
            holder.tvTitle?.text = setTitleLayout(context,it)
            holder.tvDate?.text = context.getString(R.string.order_expiryDate)+": "+(if (null==it.endTime) "--" else DateUtil.getOrderDate(it.endTime))
            //不显示可以使用范围了
            holder.tvAddress?.text = if (StrUtil.isEmpty(it.merchantNameList)) context.getString(R.string.coupon_allStationUse) else it.merchantNameList
        }

        selectHelper.bindViewHolder(holder)
    }

    override fun getItemCount(): Int {
        return dataList.size
    }


    /**
     * 设置优惠券标题+优惠内容
     *
     * @param info 优惠券
     */
    private fun setTitleLayout(mContext:Context,info: CouponRes):String{
        return if (null!=info.deduction)
            DataHelperEx.couponDeductTx(mContext,symbolTx,info.deduction)
        else
            "--"
    }
}